package com.yswee.mallnavi.model.common;


import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.utils.DeviceUtils;
import com.yswee.mallnavi.context.ApiConstant;
import com.yswee.mallnavi.context.ContextConstant;
import com.yswee.mallnavi.entity.common.AdsEntity;
import com.yswee.mallnavi.entity.common.VersionEntity;
import com.yswee.mallnavi.model.BaseModel;
import com.yswee.mallnavi.parser.BaseGsonParser;
import com.yswee.mallnavi.parser.BaseListGsonParser;
import com.yswee.mallnavi.parser.VoidParser;

import org.json.JSONArray;

import java.util.HashMap;

public class CommonModel extends BaseModel<Void> {

    public CommonModel(ITaskContext context) {
        super(context);
    }

    @Override
    protected IParser<Void> createParser() {
        return new VoidParser();
    }

    public void getVersion(final Callback<VersionEntity> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_VERSION).
            append("?type=").append(ContextConstant.getOrientationType());
        HashMap<String, Object> param = new HashMap<>();
        postData(url, param, new VersionParser(), c);
    }

    public void sendHeart(final Callback<Void> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_HEART);
        HashMap<String, Object> param = new HashMap<>();
        param.put("serialNo", ContextConstant.SERIALNO);
        param.put("version", ContextConstant.VERSION);
        param.put("ip", DeviceUtils.getIpAddress());
        postData(url, param, new VoidParser(), c);
    }

    public void getBannerList(final Callback<BaseArrayData<AdsEntity>> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_BANNERS).
            append("?type=").append(ContextConstant.getOrientationType());
        getData(url, new AdsListParser(), c);
    }

    public void getAdsList(final Callback<BaseArrayData<AdsEntity>> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_ADS).
            append("?type=").append(ContextConstant.getOrientationType());
        getData(url, new AdsListParser(), c);
    }

    private class VersionParser extends BaseGsonParser<VersionEntity> {

    }

    private class AdsListParser extends BaseListGsonParser<AdsEntity> {
        @Override
        public BaseArrayData<AdsEntity> parseData(String json) {
            mIsOk = true;
            JSONArray ja = asJSONArray(json);
            return parseData(ja);
        }
    }

}
